package test7;

import java.util.Arrays;

public class Demo7 {
    public static void main(String[] args) {
        int[] res = merge(new int[]{1, 4, 6, 0, 0, 0}, 3, new int[]{2, 5, 7}, 3);
        System.out.println(Arrays.toString(res));
    }

    public static int[] merge(int[] nums1, int m, int[] nums2, int n) {
        int l1 = 0, l2 = 0, i = 0;
        int[] res = new int[m + n];

        while (i < m + n) {
            if (l1 == m) {
                for (int j = l2; j < n; j++) {
                    res[i++] = nums2[j];
                }
                break;
            }
            if (l2 == n) {
                for (int j = l1; j < m; j++) {
                    res[i++] = nums1[j];
                }
                break;
            }
            if (nums1[l1] > nums2[l2]) {
                res[i++] = nums2[l2++];
            } else {
                res[i++] = nums1[l1++];
            }
        }

        return res;
    }
}
